<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.userName"
        placeholder="姓名"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.account"
        placeholder="账号"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.department"
        placeholder="部门"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.status" placeholder="是否在职">
        <el-option
          v-for="item in incumbencys"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="listQuery.permission" placeholder="是否管理员">
        <el-option
          v-for="item in permissions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >添加</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleSync"
      >同步用户</el-button>
      <!-- <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        Export
      </el-button>-->
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="工号" min-width="70px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" min-width="100px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <el-tag>{{ row.userName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="账号" min-width="100px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.account }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门" min-width="350px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.department }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证" min-width="180px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.cardId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="密码" min-width="100px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.password }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职时间" min-width="120px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.entryDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职时间" min-width="120px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.leaveDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否离职" min-width="100px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.status==1?'在职':'离职' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" min-width="100px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
          <span>{{ row.updatedBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updatedTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">修改密码</el-button>
          <el-tooltip :content="'权限开关'" placement="top">
            <el-switch
              v-model="row.permission"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handlePermission(row)"
            ></el-switch>
          </el-tooltip>
          <el-button type="primary" size="mini" v-if="row.type==1" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
        style="width: 500px; margin-left:50px;"
      >
        <el-form-item label="ID" prop="id" style="display: none">
          <el-input v-model="temp.sid" />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="temp.newPassword" show-password />
        </el-form-item>
        <el-form-item label="确定密码" prop="doublePassword">
          <el-input v-model="temp.doublePassword" show-password />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">清空</el-button>
        <el-button type="primary" @click="updateData()">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增" :visible.sync="dialogCreateFormVisible">
      <el-form
        ref="dataCreateForm"
        :rules="createRules"
        :model="createTemp"
        label-position="left"
        label-width="100px"
        style="width: 500px; margin-left:50px;"
      >
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="createTemp.userName" />
        </el-form-item>
        <el-form-item label="用户账号" prop="account">
          <el-input v-model="createTemp.account" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input v-model="createTemp.department" />
        </el-form-item>
        <el-form-item label="密码" prop="department">
          <el-input v-model="createTemp.password" show-password />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCreateFormVisible = false">清空</el-button>
        <el-button type="primary" @click="createData()">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="提示" :visible.sync="dialogDelVisible" width="30%" :before-close="handleClose">
      <span>
        确定需要删除
        <el-tag>{{temp.userName}}</el-tag>吗？
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDelVisible = false">取消</el-button>
        <el-button type="primary" @click="delData">删除</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { pageList, createUser, updatePassword, deleteUser, syncUser, updatePermission } from '@/api/user'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination


export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter (status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter (type) {
      return calendarTypeKeyValue[type]
    }
  },
  data () {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        limit: 10,
        userName: '',
        account: '',
        department: ''
      },
      permissions: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 'true',
          label: '是'
        },
        {
          value: 'false',
          label: '否'
        }
      ],
      incumbencys: [
        {
          value: '',
          label: '全部'
        },
        {
          value: '1',
          label: '在职'
        },
        {
          value: '2',
          label: '离职'
        }
      ],
      temp: {
        sid: '',
        password: '',
        newPassword: '',
        doublePassword: ''
      },
      createTemp: {
        userName: '',
        account: '',
        department: '',
        password: '123456'
      },
      dialogFormVisible: false,
      dialogCreateFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogDelVisible: false,
      rules: {
        newPassword: [{ required: true, message: '新密码必填', trigger: 'change' }, { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }],
        doublePassword: [{ required: true, message: '确定密码必填', trigger: 'blur' }, { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }]
      },
      createRules: {
        userName: [{ required: true, message: '用户名称必填', trigger: 'change' }],
        account: [{ required: true, message: '用户账号必填', trigger: 'blur' }],
        department: [{ required: true, message: '部门必填', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      pageList(this.listQuery).then(response => {
        this.list = response.result.records
        this.total = response.result.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter () {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus (row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    resetTemp () {
      this.temp = {
        sid: '',
        password: '',
        newPassword: '',
        doublePassword: ''
      }
    },
    resetCreateTemp () {
      this.createTemp = {
        userName: '',
        account: '',
        department: '',
        password: '123456'
      }
    },
    handleCreate () {
      this.resetCreateTemp()
      this.dialogStatus = 'create'
      this.dialogCreateFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataCreateForm'].clearValidate()
      })
    },
    createData () {
      this.$refs['dataCreateForm'].validate((valid) => {
        if (valid) {
          createUser(this.createTemp).then(() => {
            this.getList()
            this.dialogCreateFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleSync () {
      syncUser().then(() => {
        this.$notify({
          title: 'Success',
          message: '同步完成',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleUpdate (row) {
      this.temp = Object.assign({}, {})
      this.temp.sid = row.id
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let tempData = Object.assign({}, this.temp)

          if (tempData.newPassword != tempData.doublePassword) {
            this.$notify({
              title: '警告',
              message: '密码不一致',
              type: 'warning',
              duration: 2000
            })
            return
          }

          delete tempData.doublePassword

          updatePassword(tempData).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '密码修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handlePermission (row) {
      updatePermission(row).then(() => {
        this.getList()
        this.dialogCreateFormVisible = false
        this.$notify({
          title: 'Success',
          message: '权限修改成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleClose (done) {
      done()
    },
    handleDelete (row) {
      this.dialogDelVisible = true
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'delete'
    },
    delData () {
      this.dialogDelVisible = false
      const tempData = Object.assign({}, this.temp)
      deleteUser(tempData).then(() => {
        this.getList()
        this.$notify({
          title: 'Success',
          message: 'Delete Successfully',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
